<?php

namespace Tbf\Bundle\TbfBundle\Tests\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Tbf\Bundle\TbfBundle\DependencyInjection\Compiler\SubscriberTagCompilerPass;
use Tbf\Bundle\TbfBundle\Tests\TbfBundleTestCase;

class SubscriberTagCompilerPassTest extends TbfBundleTestCase
{
    public function testProcess()
    {
        $container = new ContainerBuilder();
        $compiler_pass = new SubscriberTagCompilerPass();
        $container->setDefinition('test1',new Definition('Tbf\Bundle\TbfBundle\Tests\DependencyInjection\test1'))
            ->addTag('tbf.event_dispatcher');
        $container->setDefinition('test1_subscriber',new Definition('Tbf\Bundle\TbfBundle\Tests\DependencyInjection\test1_subscriber'))
            ->addTag('test1.subscriber');
        $compiler_pass->process($container);
        $container->compile();
        $event = new GenericEvent();
        $container->get('test1')->dispatch('test_event',$event);
        $this->assertTrue($event->hasArgument('has_call'));
        $this->assertEquals(1,$event->getArgument('has_call'));
    }
    public function testCustomSubscriberTagName()
    {
        $container = new ContainerBuilder();
        $compiler_pass = new SubscriberTagCompilerPass();
        $container->setDefinition('test1',new Definition('Tbf\Bundle\TbfBundle\Tests\DependencyInjection\test1'))
            ->addTag('tbf.event_dispatcher',array('subscriber_tag'=>'test1.s'));
        $container->setDefinition('test1_subscriber',new Definition('Tbf\Bundle\TbfBundle\Tests\DependencyInjection\test1_subscriber'))
            ->addTag('test1.s');
        $compiler_pass->process($container);
        $container->compile();
        $event = new GenericEvent();
        $container->get('test1')->dispatch('test_event',$event);
        $this->assertTrue($event->hasArgument('has_call'));
        $this->assertEquals(1,$event->getArgument('has_call'));
    }
}
class test1 extends EventDispatcher
{
}
class test1_subscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array('test_event'=>'onEvent');
    }
    public function onEvent(GenericEvent $event)
    {
        $event->setArgument('has_call',1);
    }
}
